package com.xju.controller.user;
import com.xju.entity.User;
import com.xju.service.UserService;
import com.xju.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/login")
public class UserLoginServlet  extends HttpServlet{

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取参数
        req.setCharacterEncoding("UTF-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");

        //调用service
        UserService userService = new UserServiceImpl();
        User user=userService.login(username,password);
        if(user!=null){
            //登录成功 用户信息存放session
            req.getSession().setAttribute("user",user);
            req.getRequestDispatcher("/pages/starter.jsp").forward(req,resp);
        }
        else{ 
            //账号密码有误
            req.setAttribute("msg","账号或密码有误");
            req.getRequestDispatcher("/pages/login.jsp").forward(req,resp);
        }
    }
}


































